package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishFlavorService dishFlavorService;

    @PostMapping
    public R addDish(@RequestBody DishDto dishDto){
        dishService.addFlavor(dishDto);
        return R.success("添加成功");
    }


    @GetMapping("/page")
    public R pageDish(int page,int pageSize,String name){
        Page<Dish> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Dish> dqw = new LambdaQueryWrapper<>();
        dqw.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        dqw.orderByDesc(Dish::getUpdateTime);
        dishService.page(pageInfo,dqw);
        long total = pageInfo.getTotal();
        List<Dish> records = pageInfo.getRecords();
        Map map = new HashMap();
        map.put("total",total);

        List<DishDto> dishDtos = new ArrayList<>();
        for (Dish dish : records) {
            Long categoryId = dish.getCategoryId();
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            DishDto dishDto = new DishDto();
            dishDto.setCategoryName(categoryName);
            BeanUtils.copyProperties(dish,dishDto);
            dishDtos.add(dishDto);
        }
        map.put("records",dishDtos);
        return R.success(map);
    }


    @GetMapping("/{id}")
    public R getById(@PathVariable Long id){
        DishDto dishDto = dishService.getByIdFlavor(id);
        return R.success(dishDto);
    }


    @PutMapping
    public R update(@RequestBody DishDto dishDto){
        dishService.updateFlavor(dishDto);
        return R.success("修改成功");
    }


    @DeleteMapping
    public R deleteDish(@RequestParam List<Long> ids){
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.in(Dish::getId,ids);
        dishService.remove(qw);
        return R.success("删除成功");
    }


    @PostMapping("/status/{status}")
    public R updateStatusById(@PathVariable Integer status,@RequestParam List<Long> ids){
        LambdaUpdateWrapper<Dish> qw = new LambdaUpdateWrapper<>();
        qw.set(Dish::getStatus,status);
        qw.in(Dish::getId,ids);
        dishService.update(qw);
        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            dishService.updateById(dish);
        }
        return R.success("操作成功");
    }


    @GetMapping("/list")
    public R<List<DishDto>> list(Long categoryId){
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(Dish::getCategoryId,categoryId);
        List<Dish> list = dishService.list(qw);
        List<DishDto> collect = list.stream().map(dish -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            LambdaQueryWrapper<DishFlavor> dqw = new LambdaQueryWrapper<>();
            dqw.eq(DishFlavor::getDishId, dishDto.getId());
            List<DishFlavor> flavors = dishFlavorService.list(dqw);
            dishDto.setFlavors(flavors);
            return dishDto;
        }).collect(Collectors.toList());
        return R.success(collect);
    }
}
